package it.antonio.ilpizze.func.core;

import it.antonio.ilpizze.func.Mapper;
import it.antonio.ilpizze.func.Predicate;

import java.util.Map;

public abstract class Mappers extends Functions implements Core {
	@Override
	public final void mustImplementMappers() {
	}
	
	
	public <A,B,C> Mapper<A,C> compose(final Mapper<A,B> map1, final Mapper<B, C> map2){
		return new Mapper<A, C>() {

			@Override
			public C map(A in) {
				return map2.map(map1.map(in));
			}
		};
	}
	
	public Mapper<Object, String> toStringMapper(){
		return new Mapper<Object, String>() {

			@Override
			public String map(Object in) {
				return in == null ? null : in.toString();
			}
			
			
		};
	}
	public <K,V> Mapper<K,V> forMap(final Map<K,V> map){
		return new Mapper<K,V>() {

			@Override
			public V map(K in) {
				return map.get(in);
			}
			
			
		};
	}
	
	public <T> Mapper<T,Boolean> forPredicate(final Predicate<T> pred){
		return new Mapper<T,Boolean>() {

			@Override
			public Boolean map(T in) {
				return pred.match(in);
			}

		};
	}
}
